<template>
	<uni-popup ref="popup" type="center" @maskClick="closeFun">
		<view style="width: 480rpx;">
			<view class="bgc-white round-md py-50 px-30">
				<M-RoundProgress width="250" activeColor="#2BB130" :ratio="resetNumber">
					<view class="flex flex-column items-center ">
						<view class="mb-10 text-30">当前进度</view>
						<view v-if="istask">
							<text class="text-32 text-green">{{examdata.list.length}}</text>
							<text class="mx-10 text-24">/</text>
							<text class="text-24">{{examdata?.total}}</text>
						</view>
						<view v-else>
							<text class="text-32 text-green">{{isexam ? (examdata?.list?.length || 0) : statisticsdata.totalNumber - statisticsdata?.noAnswer}}</text>
							<text class="mx-10 text-24">/</text>
							<text class="text-24">{{isexam ? examdata?.total : (store_config.questConfig.slider || statisticsdata.totalNumber)}}</text>
						</view>
					</view>
				</M-RoundProgress>
				
				<view class="flex items-center justify-center mt-40">
					<text class="text-26 mr-10">已用时间</text>
					<c_timedown size="18" :flash="false"></c_timedown>
				</view>
				
				<view class="text-22 text-gray-80 mt-15 text-center mt-50">
					<text v-if="isexam && (examdata.list.length === examdata?.total)">考试时间未到</text>
					<template v-else-if="istask">
						<template v-if="examdata?.total - examdata.list.length > 0">你有{{examdata?.total - examdata.list.length}}道未完成的题目</template>
						<template v-else>你已答完所有题目</template>
					</template>
					<template v-else>
						<template v-if="statisticsdata?.noAnswer">
							<text>你有</text>
							<text class="text-30 text-red">{{store_config.questConfig.slider ? store_config.questConfig.slider - (statisticsdata.totalNumber - statisticsdata.noAnswer) : statisticsdata?.noAnswer}}</text>
							<text>道未完成的题目</text>
						</template>
						<template v-else>
							你已答完所有题目
						</template>
					</template>
					<text>，请注意检查试卷</text>
				<!-- {{isexam && (examdata.list.length === examdata?.total) ? '考试时间未到' : (statisticsdata?.noAnswer ? '你有‘'+statisticsdata?.noAnswer+'道未完成的题目' : '') + '，请注意检查试卷'}} -->
				</view>
				<view class="flex justify-center items-center mt-40">
					<view class="text-26 text-gray mr-40" @click="submitEvent">确认交卷</view>
					<view class="bgc-blue round-30 py-15 text-26 text-white text-center" style="width: 200rpx;" @click="closeFun">{{isexam ? '继续考试' : '继续练习' }}</view>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script setup>
	import { nextTick, ref, getCurrentInstance, onMounted, watch, computed } from 'vue'
	import { navigate } from '@/common/hooks/useNavigate.js'
	import c_timedown from './timedown.vue'
	
	const { proxy } = getCurrentInstance()
	const { store_config } = proxy.$store()
	
	const activeMode = ref('1')
	
	const props = defineProps({
		modelValue: Boolean,
		reqdata: Object,
		isexam: Boolean,
		istask: Boolean,
		examdata: Object,
		questIndex: Number
	})
	
	const popup = ref()
	
	const statisticsdata = ref({})
	const detaildata = ref({})
	const tasksttatisdata = ref({})
	
	const emits = defineEmits(['begin', 'update:modelValue', 'success'])
	
	function editExercise() {
		emits('begin', activeMode.value)
		closeFun()
	}
	
	// const percentage = computed(() => {
	// 	if(props.isexam){
	// 		return props.examdata?.list?.length / props.examdata?.total * 100
	// 	} else{
	// 		console.log('计算属性内的百分比', ((statisticsdata.totalNumber - statisticsdata?.noAnswer) / statisticsdata.totalNumber * 100))
	// 		// if(store_config.questConfig.slider){
	// 		// 	return (store_config.questConfig.slider - (statisticsdata.totalNumber - statisticsdata?.noAnswer)) / statisticsdata.totalNumber * 100 || 0
	// 		// } else{
	// 		// 	return ((statisticsdata.totalNumber - statisticsdata?.noAnswer) / statisticsdata.totalNumber * 100) || 0
	// 		// }
			
	// 		if(store_config.questConfig.slider){
	// 			return (store_config.questConfig.slider - (statisticsdata.value.totalNumber - statisticsdata.value.noAnswer)) / statisticsdata.value.totalNumber * 100 || 0
	// 		} else{
	// 			return ((statisticsdata.value.totalNumber - statisticsdata.value.noAnswer) / statisticsdata.value.totalNumber * 100) || 0
	// 		}
	// 	}
	// })
	
	const resetNumber = ref(0)
	
	watch(() => props.modelValue, async (newVal, oldVal) => {
		console.log('组件监听value', newVal)
		if(newVal){
			if(props.isexam){
				detaildata.value = await proxy.$http.get('examination/user/exam-candidate/exam', props.reqdata)
				resetNumber.value = props.examdata?.list?.length / props.examdata?.total * 100
			} else if(props.istask){
				resetNumber.value = props.examdata?.list?.length / props.examdata?.total * 100
				console.log(11111111, props.examdata)
			} else{
				statisticsdata.value = await proxy.$http.get('examination/user/exercise-member-question/statistics', props.reqdata)
				if(store_config.questConfig.slider){
					resetNumber.value = (statisticsdata.value.totalNumber - statisticsdata.value.noAnswer) / store_config.questConfig.slider * 100 || 0
					console.log('重新计算完的1', resetNumber.value)
				} else{
					resetNumber.value = (statisticsdata.value.totalNumber - statisticsdata.value.noAnswer) / statisticsdata.value.totalNumber * 100 || 0
					console.log('重新计算完的2', resetNumber.value)
				}
				// resetNumber.value = ((statisticsdata.value.totalNumber - statisticsdata.value?.noAnswer) / statisticsdata.value.totalNumber * 100) || 0
				
			}
			popup.value.open()
		} else{
			popup.value && popup.value.close()
		}
	}, { immediate: true, deep: true})
	
	const closeFun = () => {
		emits("update:modelValue")
	}
	
	// 提交试卷
	async function submitEvent(){
		if(props.isexam){
			uni.showLoading({ title: '考试提交中'})
			props.examdata.list.length && await proxy.$http.post('examination/user/exam-candidate-log/batch-answer', {...props.reqdata, ...{answers: props.examdata.list}})
			await proxy.$http.post('examination/user/exam-candidate-log/submit', props.reqdata)
			uni.hideLoading()
			uni.showToast({ title:'交卷成功', icon:"none" })
			uni.redirectTo({ url: `/pages/exam/result?examID=${props.reqdata.examId}&type=${props.reqdata.sourceType}&sourceID=${props.reqdata.sourceId}` })
		} else if(props.istask){
			emits('success')
		} else{
			// 交卷完成之后改变章节的状态
			await proxy.$http.post('examination/user/exercise-member/submit', props.reqdata)
			let attr = {
				bankId: props.reqdata.bankId,
				sectionId: props.reqdata.sectionId,
			}
			
			const query = {
				exerciseId: props.reqdata.exerciseId,
				sourceType: props.reqdata.sourceType,
				sourceId: props.reqdata.sourceId,
				exerciseLogId: props.reqdata.exerciseLogId,
				bankId: props.reqdata.bankId,
				sectionId: props.reqdata.sectionId,
			}
			navigate('/pages/exercise/result', {params: query, isAuth: true, isReplace: true })
			uni.$emit('updateTreeStatusInstance', attr)
		}
	}
	
</script>